package management;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.WindowConstants;

import dao.DaoFactory;
import dao.UserDao;

public class admin extends JFrame  implements ActionListener {

	 boolean isGo;
     public admin() {
        init();
        setTitle("彩票购买抽奖程序");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		super.setLocation((width - 400) / 2, (height - 350) / 2);
	
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void init() {
    	
    	ArrayList<Integer> arr=new ArrayList();;
    	for(int i=0;i<37;i++)
    		arr.add(i);
    	ArrayList<Integer> arr1=new ArrayList();
    	 model1 = new SpinnerListModel(arr);
    	 model2 = new SpinnerListModel(arr);
    	 model3 = new SpinnerListModel(arr);
    	 model4 = new SpinnerListModel(arr);
    	 model5 = new SpinnerListModel(arr);
    	 model6 = new SpinnerListModel(arr);
    	 model7 = new SpinnerListModel(arr);
    	
    	 label1 =  new JLabel();
    	 num1 = new JSpinner(model1);
    	 num2 = new JSpinner(model2);
    	 num3 = new JSpinner(model3);
    	 num4 = new JSpinner(model4);
    	 num5 = new JSpinner(model5);
    	 num6 = new JSpinner(model6);
    	 num7 = new JSpinner(model7);
    	 label2 = new JLabel();
    	 label3 = new JLabel();
    	 label4 = new JLabel();
    	 label5 = new JLabel();
    	 label6 = new JLabel();
    	 label7 = new JLabel();
    	 label8 = new JLabel();
    	 bt1 = new JButton();
    	 bt2 = new JButton();
    	 bt3 = new JButton();
    	 bt4 = new JButton();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        getContentPane().setBackground(Color.CYAN);
        label1.setFont(new Font("宋体", 1, 20)); // NOI18N
        label1.setText("生成中奖的号数");

        label2.setFont(new Font("宋体", 1, 14)); 
        label2.setText("1");

        label3.setFont(new Font("宋体", 1, 14)); 
        label3.setText("2");

        label4.setFont(new java.awt.Font("宋体", 1, 14)); 
        label4.setText("3");

        label5.setFont(new Font("宋体", 1, 14)); 
        label5.setText("4");

        label6.setFont(new Font("宋体", 1, 14)); 
        label6.setText("5");

        label7.setFont(new Font("宋体", 1, 14)); 
        label7.setText("6");

        label8.setFont(new Font("宋体", 1, 14)); 
        label8.setText("7");
        

        bt1.setText("start");
        
        bt2.setText("end");
       
        bt3.setText("查看");
       
        bt4.setText("返回");
        
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(label1))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(num1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2)
                                    .addComponent(num5, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label6))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label7)
                                    .addComponent(num6, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label3)
                                    .addComponent(num2,GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label8)
                                    .addComponent(num7,GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label4)
                                    .addComponent(num3, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label5)
                                    .addComponent(num4, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    
                                   )))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt2)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt3)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(num1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label2)
                    .addComponent(label3)
                    .addComponent(label4)
                    .addComponent(label5))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(num5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label6)
                    .addComponent(label7)
                    .addComponent(label8)
                    )
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(bt1)
                    .addComponent(bt2)
                   .addComponent(bt3)
                    .addComponent(bt4))
                .addContainerGap())
        );

        pack();
        this.setVisible(true);
    	this.setLocation(500, 400);
        bt1.addActionListener(this);
       bt2.addActionListener(this);
       bt3.addActionListener(this);
       bt4.addActionListener(this);
       Cursor cu = new Cursor(Cursor.HAND_CURSOR);
		 this.setCursor(cu);
		 this.go();
    }


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//String s =e.getActionCommand();
		if(e.getSource()==bt1){
			isGo =true;
			bt1.setEnabled(false);
			bt2.setEnabled(true);
		}
		else if(e.getSource()==bt2){
			isGo =false;
			bt1.setEnabled(true);
			bt2.setEnabled(false);
			MatchNumber();
			
		}
		else if(e.getSource()==bt3)
		{
			new Match();
		}
		else {
			
			 this.setVisible(false);
		}
	}
	
	public void go(){
		while(true){
			if(isGo){

				ArrayList<Integer> list = new ArrayList<Integer>();
                Random random = new Random();
                int i = 0, num = 0;
                while (i < 7) {
                        num = random.nextInt(35) + 1;
                        if (!list.contains(num)) {
                                list.add(num);
                        } else continue;
                        i++;
                }
                num1.setValue(list.remove(0));
                num2.setValue(list.remove(0));
                num3.setValue(list.remove(0));
                num4.setValue(list.remove(0));
                num5.setValue(list.remove(0));
                num6.setValue(list.remove(0));
                num7.setValue(list.remove(0));

			}

			try{
				Thread.sleep(50);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		
	}
	
	
	public void MatchNumber() {
		
		
		ArrayList<Integer> list1 = new ArrayList<Integer>();
		
		int s1=Integer.parseInt(num1.getValue().toString());
		list1.add(s1);
		
		int s2=Integer.parseInt(num1.getValue().toString());
		list1.add(s2);
		
		int s3=Integer.parseInt(num1.getValue().toString());
		list1.add(s3);
		
		int s4=Integer.parseInt(num1.getValue().toString());
		list1.add(s4);
		
		int s5=Integer.parseInt(num1.getValue().toString());
		list1.add(s5);
		
		int s6=Integer.parseInt(num1.getValue().toString());
		list1.add(s6);
		
		int s7=Integer.parseInt(num1.getValue().toString());
		list1.add(s7);
		
		UserDao userDao = DaoFactory.getUserDaoInstance();
    	try {
			userDao.LotteryDraw(list1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	

	SpinnerModel model1;
	SpinnerModel model2;
	SpinnerModel model3;
	SpinnerModel model4;
	SpinnerModel model5 ;
	SpinnerModel model6;
	SpinnerModel model7 ;
	
	JLabel label1  ;
	JSpinner num1 ;
	JSpinner num2 ;
	JSpinner num3 ;
	JSpinner num4 ;
	JSpinner num5 ;
	JSpinner num6;
	JSpinner num7;
	JLabel label2 ;
	JLabel label3 ;
	JLabel label4 ;
	JLabel label5;
	JLabel label6;
	JLabel label7 ;
	JLabel label8;
	JButton bt1 ;
	JButton bt2 ;
	JButton bt3 ;
	JButton bt4 ;
}

	

